﻿using MY.ShoppingBackstage.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace MY.ShoppingBackstage.BLL
{
    public class UserInfoService : BaseService<UserInfo>
    {

        /// <summary>
        /// 分页查询（显示列表）
        /// </summary>
        /// <returns></returns>
        public List<UAddress> GetQuery(string name)
        {
            using (var db = new WaiShopEntities())
            {
                var result = from a in db.UserInfo
                             join b in db.User_Address
                             on a.Id equals b.UserId
                             where a.UserName.Contains(name)
                             select new UAddress()
                             {
                                 Id = a.Id,
                                 UserName = a.UserName,
                                 RealName = a.RealName,
                                 UserSex = a.UserSex,
                                 Phone = a.Phone,
                                 Address = b.Address,
                                 CreateTime = a.CreateTime,
                                 Status = a.Status
                             };
                return result.ToList();
            }
        }



        /// <summary>
        /// 查询地址
        /// </summary>
        /// <returns></returns>
        public List<UAddress> GetAddress()
        {
            using (var db = new WaiShopEntities())
            {
                //select Province,City,District from dbo.User_Address group by Province,City,District 
                var result = from a in db.User_Address
                             group a by new { a.Province, a.City, a.District } into g
                             select new UAddress()
                             {
                                 Province = g.Key.Province,
                                 City = g.Key.City,
                                 District = g.Key.District,
                             };
                return result.ToList();
            }
        }




        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="AId"></param>
        /// <returns></returns>
        public bool DeleteUser(int id)
        {
            //代码执行完，则释放资源   声明一个实例
            using (var db = new WaiShopEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        UserInfo userInfo = db.UserInfo.FirstOrDefault(a => a.Id == id);
                        //删除用户表
                        db.UserInfo.Remove(userInfo);
                        db.SaveChanges();
                        //删除地址表
                        User_Address user_Address = db.User_Address.FirstOrDefault(a => a.UserId == id);
                        db.User_Address.Remove(user_Address);
                        db.SaveChanges();
                        tran.Commit();
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }


        /// <summary>
        /// 修改用户及地址
        /// </summary>
        /// <param name="uAddress"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool UpdateUser(UAddress uAddress)
        {
            //代码执行完，则释放资源   声明一个实例
            using (var db = new WaiShopEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //修改用户表
                        db.Entry(uAddress).State = EntityState.Modified;
                        db.SaveChanges();
                        //修改地址表
                        User_Address user_Address = db.User_Address.FirstOrDefault(a => a.UserId == uAddress.Id);
                        db.Entry(user_Address).State = EntityState.Modified;
                        db.SaveChanges();
                        tran.Commit();
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }


        /// <summary>
        /// 修改状态（禁用/启用）
        /// </summary>
        /// <param name="Uid"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool UpdateStatus(int Uid,int Status)
        {
            //代码执行完，则释放资源   声明一个实例
            using (var db = new WaiShopEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        UserInfo userInfo = this.Query(a => a.Id == Uid);
                        userInfo.Status = Status;
                        this.Update(userInfo);
                        db.SaveChanges();
                        tran.Commit();
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }


    }
}
